<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>defineProperty方法用法</title>
</head>

<body>

    <div id="app"></div>

    <script>
        const obj = {};
        Object.defineProperty(obj, 'property1', {
            value: 42,
            // writable为true时，value是不起作用  false取值为value
            writable: false,
        })
        obj.property1 = 77;
        console.log(obj.property1);

        console.dir(Object.defineProperty)
        console.log(Object.defineProperty)

        console.log("自定义setters和getters");

        function Archive(){
            // 1.定义一个空对象
            var temperature = null ;
            // 2.定义一个空数组
            var archive = [];
            // 3.给这个实例化的对象添加temperature属性， 存值器   取值器
            Object.defineProperty(this,'temperature',{
                // 取值器就是返回temperature的变量值
                get:function(){
                    console.log('get');
                    return temperature;
                },
                // 存值器 就是将新的value值复制给temperature  archive数组存入每次存值器的值
                set:function(value){
                    temperature = value;
                    archive.push({val:temperature});
                }
            });
            // 实例化对象上挂载getArchive方法 返回archive数组
            this.getArchive = function(){return archive;};
        };
        // 实例化   有一个属性一个方法
        var arc = new Archive();
        console.log('--------');
        console.log(arc);
        console.log('--------');
        console.dir(Archive)
        console.log('xxxxxxxx');
        console.log(arc.temperature);
        
        arc.temperature = 11;
        console.log(arc.temperature);
        arc.temperature =13;
        console.log(arc.temperature);
        console.log(arc.getArchive());
        
</script>

</body>

</html>